<?php
namespace app\admin\controller;
use think\Controller;
class Base extends  Controller {

    public $admin;
    public function _initialize() {
        // 判定用户是否登录
        $isLogin = $this->isLogin();
        if(!$isLogin) {
             $this->redirect(url('login/index'));
        }
    }

    //判定是否登录
    public function isLogin() {
        // 获取sesssion
        $user = $this->getLoginUser();
        if($user && $user->id) {
            return true;
        }
        return false;

    }

    public function getLoginUser() {
        if(!$this->admin) {
            $this->admin = session('o2o_admin', '', 'admin');
        }
        return $this->admin;
    }

    public function status() {
        // 获取值
        $data = input('get.');
        // 利用tp5 validate 去做严格检验  id  status
        if(empty($data['id'])) {
            $this->error('id不合法');
        }
        if(!is_numeric($data['status'])) {
            $this->error('status不合法');
        }

        $validate=validate($data['model']);
        if (!$validate->scene('status')->check($data)){
            $this->error($validate->getError());
        }
        //echo $model;exit;
        $res = model($data['model'])->save(['status'=>$data['status']], ['id'=>$data['id']]);
        if($res) {
            $this->success('更新成功');
        }else {
            $this->error('更新失败');
        }
    }

    public function listorder($id,$listorder,$model)
    {
        $data=['id' =>$id,'listorder'=>$listorder,'model'=>$model];
        $validate=validate($data['model']);
        if (!$validate->scene('listorder')->check($data)){
            $this->error($validate->getError());
        }
        $res= model($data['model'])->save(['listorder'=>$data['listorder']],['id' =>$data['id']]);
        if ($res){
            $this->result($_SERVER['HTTP_REFERER'],1,'更新成功');
        }else{
            $this->result($_SERVER['HTTP_REFERER'],0,'更新失败');
        }
    }
}